<template>
    <div>
        <div style="margin-bottom: 10px">
            <i-button size="default"   type="primary" style="margin-right: 10px" @click="btn_update">刷新</i-button>
        </div>
        <i-table size="default"   border stripe  :columns="format" :data="show_list" :height="sharedState.tableHeight" ></i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" show-total/>
        </div>
    </div>
</template>

<script>

import { init_log_list } from  '@/api'
import store from '@/store'
import {post} from '@/api/http'


export default {
    data() {
        return {
            sharedState: store.state,
            format: [
                {
                    title: '序号',key: 'id'
                },
                {
                    title: '日志',key: 'msg'
                },
                {
                    title: '创建时间',key: 'create_time'
                },
            ],
            total_list: [],
            show_list: [],
            // 分页
            total: 0,
            pageSize: 15,
        }
    },
    methods: {
        changePage(index) {
            let _start = (index -1) * this.pageSize
            let _end = index * this.pageSize
            this.show_list = this.total_list.slice(_start,_end)
        },
        // 点击刷新事件
        btn_update(){
            this.refresh()
        },
        refresh() {
            post(init_log_list).then( (response) => {
                this.total_list = response.data
                this.total = response.data.length
                if(this.total < this.pageSize) {
                    this.show_list = this.total_list
                }else {
                    // 修改改数据之后显示回到第一页的bug，改为停留在当前页
                    let currentPage = this.$refs.page.currentPage
                    let _start = (currentPage-1) * this.pageSize
                    let _end = currentPage * this.pageSize
                    this.show_list = this.total_list.slice(_start,_end)
                }
            })
        }
    },
    mounted: function() {
        setTimeout(() =>{
            this.refresh();
        },200); 
    }
}
</script>
<style scoped>

</style>